//
//  UIView+XMBUFF.m
//  LeGou
//
//  Created by lxm on 2022/12/6.
//

#import "UIView+XMBUFF.h"
#import <objc/runtime.h>

@implementation UIView (XMBUFF)

static const char callBack;

+(void)load {
    [super load];
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        [self swizzleInstanceMethod:self original:@selector(layoutSubviews) swizzled:@selector(swizzle_layoutSubviews)];
    });
}

+ (void)swizzleInstanceMethod:(Class)target original:(SEL)originalSelector swizzled:(SEL)swizzledSelector {
    Method originMethod = class_getInstanceMethod(target, originalSelector);
    Method swizzledMethod = class_getInstanceMethod(target, swizzledSelector);
    method_exchangeImplementations(originMethod, swizzledMethod);
}

- (void (^)(void))layoutSubViewsCallBack {
    id sth = objc_getAssociatedObject(self, &callBack);
    return sth;
}

- (void)setLayoutSubViewsCallBack:(void (^)(void))layoutSubViewsCallBack {
    objc_setAssociatedObject(self, &callBack, layoutSubViewsCallBack,  OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

//-(void)setLayoutSubViewsCallBack:(void (^)(void))layoutSubViewsCallBack } {
//    return (ButtonImageLocation)[objc_getAssociatedObject(self, &callBack) integerValue];
//}

-(void)swizzle_layoutSubviews {
    [self swizzle_layoutSubviews];
    if (self.layoutSubViewsCallBack) {
        self.layoutSubViewsCallBack();
    }
}
@end


//@implementation UIButton (XMBUFF)
//+(void)load {
//    [super load];
//    static dispatch_once_t onceToken;
//    dispatch_once(&onceToken, ^{
//        [self swizzleInstanceMethod:self original:@selector(layoutSubviews) swizzled:@selector(swizzle_layoutSubviews)];
//    });
//}
//
//+ (void)swizzleInstanceMethod:(Class)target original:(SEL)originalSelector swizzled:(SEL)swizzledSelector {
//    Method originMethod = class_getInstanceMethod(target, originalSelector);
//    Method swizzledMethod = class_getInstanceMethod(target, swizzledSelector);
//    method_exchangeImplementations(originMethod, swizzledMethod);
//}
//
//-(void)swizzle_layoutSubviews {
//    [self swizzle_layoutSubviews];
//    if (self.layoutSubViewsCallBack) {
//        self.layoutSubViewsCallBack();
//    }
//}
//@end
